package com.cyss.greenball.main;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;
import com.cyss.greenball.dao.SaveMsgDao;
import com.cyss.greenball.util.ActivityUtil;
import com.cyss.greenball.util.BaseService;
import com.cyss.greenball.util.SystemUtil;
import com.cyss.greenball.view.MyMapViewFactory;
import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;
import com.lianzt.util.Base64;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyMapActivity extends BaseActivity {

	private TextView myMapTitle;
	private ImageView myMapView;
	private Button myMapPrevious;
	private Button myMapNext;
	private Button uploadMap;
	private Map<String, AbstractCommonData> mapInfo;
	private int nowMapNum = 0;
	private Object[] keyArray;
	private ProgressDialog pdUpload;// 登录圆形进度条

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.my_map);
	}

	@Override
	protected void init() {
		myMapTitle = (TextView) findViewById(R.id.my_map_title);
		myMapView = (ImageView) findViewById(R.id.my_map_view);
		myMapPrevious = (Button) findViewById(R.id.my_map_previous);
		myMapNext = (Button) findViewById(R.id.my_map_next);
		uploadMap = (Button) findViewById(R.id.upload_map);
		mapInfo = SaveMsgDao.getAllValueWithKey(this);
		pdUpload = new ProgressDialog(this);
		pdUpload.setMessage("正在上传。。");
		if (mapInfo.size() > 0) {
			keyArray = mapInfo.keySet().toArray();
			setMyMapView();
		} else {
			myMapTitle.setText("本地暂无地图");
		}
		setButtonState();
	}

	private void setButtonState() {
		if ((nowMapNum == 0 && mapInfo.size() == 0)||(nowMapNum == 0 && mapInfo.size() == 1)) {
			myMapPrevious.setEnabled(false);
			myMapNext.setEnabled(false);
		} else if (nowMapNum == 0 && mapInfo.size() > 1) {
			myMapNext.setEnabled(true);
			myMapPrevious.setEnabled(false);
		} else if (mapInfo == null || mapInfo.size() == 0
				|| nowMapNum == mapInfo.size() - 1) {
			myMapNext.setEnabled(false);
			myMapPrevious.setEnabled(true);
		} else {
			myMapPrevious.setEnabled(true);
			myMapNext.setEnabled(true);
		}
	}

	private void setMyMapView() {
		myMapView.setImageBitmap(MyMapViewFactory.getBitmapByCommdata(
				SystemUtil.SCREEN_WIDTH - 40, SystemUtil.SCREEN_HEIGHT - 60,
				mapInfo.get(keyArray[nowMapNum])));
		myMapTitle.setText(keyArray[nowMapNum] + "(" + (nowMapNum + 1) + "/"
				+ mapInfo.size() + ")");
	}

	private void uploadMyMap() {
		// mapInfo.get(keyArray[nowMapNum]));
		// AbstractCommonData backAcd =
		// SystemUtil.sendCommonData(mapInfo.get(keyArray[nowMapNum]));
		// backAcd.getDataValue("head").getStringValue("response_desc");
		pdUpload.show();

		AbstractCommonData sendData = SystemUtil
				.getSendInstance(SystemUtil.SUBMIT_GREENBALL_MAP);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		MyMapViewFactory
				.getBitmapByCommdata(SystemUtil.SCREEN_WIDTH - 40,
						SystemUtil.SCREEN_HEIGHT - 60,
						mapInfo.get(keyArray[nowMapNum])).compress(
						Bitmap.CompressFormat.JPEG, 100, baos);
		byte[] data = baos.toByteArray();
		mapInfo.get(keyArray[nowMapNum]).putStringValue("map_img", Base64.encode(data));
		mapInfo.get(keyArray[nowMapNum]).putStringValue("map_name", keyArray[nowMapNum].toString());
		sendData.putDataValue("map_data", mapInfo.get(keyArray[nowMapNum]));
		sendData.putObjectValue("listener", new BaseService.ServiceListener() {

			@Override
			public void finishSendData(AbstractCommonData backAcd) {
				pdUpload.cancel();
			}
		});
		
		BaseService.addService(sendData);
		

	}

	@Override
	protected void setOnListener() {
		myMapPrevious.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				nowMapNum--;
				setButtonState();
				setMyMapView();
			}
		});

		myMapNext.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				nowMapNum++;
				setButtonState();
				setMyMapView();
			}
		});

		uploadMap.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				uploadMyMap();
			}
		});

		myMapView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AbstractCommonData dialog = DataConvertFactory
						.getInstanceEmpty();
				dialog.putStringValue("text", "是否试玩？");
				dialog.putStringValue("button1", "否");
				dialog.putStringValue("button2", "是");
				dialog.putObjectValue("button2_listener",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent intent = new Intent(MyMapActivity.this,
										WorldActivity.class);
								intent.putExtra("acdGame", DataConvertFactory
										.praseString(mapInfo
												.get(keyArray[nowMapNum])));
								startActivity(intent);
							}
						});
				ActivityUtil.Dialog(MyMapActivity.this, dialog);
			}
		});
	}

}
